package com.woniuxy.three.pms.user.service.impl;

import java.io.IOException;
import java.util.List;

import com.woniuxy.three.pms.util.QiniuFileUploadUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.woniuxy.three.pms.bean.UserBean;
import com.woniuxy.three.pms.dao.UserBeanMapper;
import com.woniuxy.three.pms.user.service.IUserService;
import com.woniuxy.three.pms.util.CacheUtil;
import org.springframework.web.multipart.MultipartFile;

@Service
public class UserServiceImpl implements IUserService {
	
	@Autowired
	private CacheUtil cacheUtil; 
	
	@Autowired
	private UserBeanMapper userBeanMapper;

	/**
	 * 查找所有用户
	 * @param pageStart
	 * @param pageSize
	 * @return
	 */
	@Override
	public PageInfo<UserBean> findall(Integer pageStart, Integer pageSize) {
		PageHelper.startPage(pageStart,pageSize);
		List<UserBean> list = userBeanMapper.findAllUser();
		PageInfo<UserBean> info = new PageInfo<>(list);
		return info;
	}

	/**
	 * 添加用户
	 * @param user
	 * @return
	 */
	@Override
	public int adduser(UserBean user) {
		return userBeanMapper.insertSelective(user);
	}

	/**
	 * 通过用户ID查找用户
	 * @param id
	 * @return
	 */
	@Override
	public UserBean finduserById(Integer id) {
		// TODO Auto-generated method stub
		return userBeanMapper.selectByPrimaryKey(id);
	}

	/**
	 * 通过验证码+用户手机号方式登录
	 * @param mobile
	 * @param verCode
	 * @return
	 */
	@Override
	public UserBean login(String mobile, String verCode) {
		// TODO Auto-generated method stub
		//查询当前验证码
		String cacheVerifyCode = cacheUtil.getValueWithKey(mobile);
		if (cacheVerifyCode!= null){
			if (cacheVerifyCode.equalsIgnoreCase(verCode)){
				return userBeanMapper.findUserByMobile(mobile);
			}
		}
		return null;
	}

		
	/***
	 * 用户个人修改本人信息的信息
	 */
	@Override
	public boolean modifyUser(UserBean user) {
		// TODO Auto-generated method stub
		int modifyResult = userBeanMapper.updateByPrimaryKeySelective(user);
		if (modifyResult > 0) {
			return true;
		}
		return false;
	}

	/**
	 * 操作员修改用户信息
	 */
	@Override
	public boolean rootModifyUser(UserBean user) {
		// TODO Auto-generated method stub
		int modifyResult = userBeanMapper.updateByPrimaryKeySelective(user);
		if (modifyResult>0) {
			return true;
		}
		return false;
	}


	/**
	 * 通过姓名关键字进行搜索用户列表
	 * @param keyword
	 * @return
	 */
	@Override
	public PageInfo<UserBean> searchUserByKeyWord(String keyword) {
		// TODO Auto-generated method stub
		PageHelper.startPage(1, 8);
		keyword = "%"+keyword+"%";
		List<UserBean> result = userBeanMapper.findUserByKeyWord(keyword);
		PageInfo<UserBean> pageInfo = new PageInfo<UserBean>(result);
		return pageInfo;
	}


	/**
	 * 通过VIP搜索，显示用户列表
	 * @param vip
	 * @return
	 */
	@Override
	public PageInfo<UserBean> searchUserByVip(String vip) {
		// TODO Auto-generated method stub
		PageHelper.startPage(1, 8);
		List<UserBean> result = userBeanMapper.finUserByVip(vip);
		PageInfo<UserBean> pageInfo = new PageInfo<UserBean>(result);
		return pageInfo;
	}

	/**
	 * 通过用户手机号+密码进行登录
	 * @param mobile
	 * @param password
	 * @return
	 */
	@Override
	public UserBean loginByMobile(String mobile, String password) {
		// TODO Auto-generated method stub
		UserBean user = userBeanMapper.findUserByMobile(mobile);
		if (user!=null) {
			if (user.getPassword().equals(password)) {
				return user;
			}
			return null;
		}
		return null;
	}

	/**
	 * 根据电话号码来找用户
	 * @param mobile
	 * @return
	 */
	@Override
	public UserBean findUserByMobile(String mobile) {
		return userBeanMapper.findUserByMobile(mobile);
	}

	/**
	 * 上传头像
	 * @param file 头像的文件
	 * @param mobile 用户的ID
	 * @return
	 * @throws IOException
	 */
	@Override
	public String uploadHeadImg(MultipartFile file, String mobile) throws IOException {
		//获取到user
		UserBean userBean = userBeanMapper.findUserByMobile(mobile);
		//调用七牛
		String uploadImg = QiniuFileUploadUtil.uploadImg(file);
		userBean.setHeadImg(uploadImg);
		//更新头像
		userBeanMapper.updateByPrimaryKeySelective(userBean);
		return QiniuFileUploadUtil.QINIU_HEAD_IMG_BUCKET_URL+"/"+QiniuFileUploadUtil.QINIU_HEAD_IMG_BUCKET_NAME+"/"+uploadImg;
	}


}
